//
// Copyright (C) 2008 Jackson Harper (jackson@ximian.com)

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//

using System;
using System.Collections.Generic;

using MySql.Data.MySqlClient;

namespace Grr.Model
{
    [Serializable]
    public class Comment
    {
        private static readonly int IdColumnIndex = 0;
        private static readonly int TextColumnIndex = 1;
        private static readonly int UserNameColumnIndex = 2;
        private static readonly int UserPictureColumnIndex = 3;
        private static readonly string QueryString = "SELECT Comments.id, Comments.text, Users.FriendlyName, Users.Picture FROM Comments LEFT JOIN Users ON Comments.user_id = Users.Username WHERE Comments.grurrah_id = ?grurrah_id ORDER BY Comments.creation_time DESC LIMIT ?limit_start, ?limit_length";
        private static readonly string InsertString = "INSERT INTO Comments (user_id, grurrah_id, creation_time, text) VALUES (?user_id, ?grurrah_id, NOW(), ?text)";
        
        private long id;
        private string text;
        private string username;
        private string picture;

        private Comment(long id, string text, string username, string picture)
        {
            this.id = id;
            this.text = text;
            this.username = username;
            this.picture = picture;
        }

        public string FriendlyUserName
        {
            get { return username; }
        }

        public string Picture
        {
            get { return picture; }
        }

        public string Text
        {
            get { return text; }
        }

        public static List<Comment> GetCommentsForGrurrah(long grurrah_id, int start, int length)
        {
            List<Comment> res = new List<Comment>();
            using (MySqlConnection con = DbEntity.OpenReadConnection())
            {
                MySqlCommand cmd = new MySqlCommand(QueryString, con);
                cmd.Parameters.Add("grurrah_id", MySqlDbType.Int64).Value = grurrah_id;
                cmd.Parameters.Add("limit_start", MySqlDbType.Int32).Value = start;
                cmd.Parameters.Add("limit_length", MySqlDbType.Int32).Value = length;
                
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    long id = reader.GetInt64(IdColumnIndex);
                    string text = reader.GetString (TextColumnIndex);
                    string username = reader.GetString (UserNameColumnIndex);
                    string picture = reader.GetString (UserPictureColumnIndex);

                    res.Add(new Comment(id, text, username, picture));
                }
            }

            return res;
        }

        public static void AddComment(string user_id, long grurrah_id, string text)
        {
            using (MySqlConnection con = DbEntity.OpenReadWriteConnection())
            {
                MySqlCommand cmd = new MySqlCommand(InsertString, con);
                cmd.Parameters.Add("grurrah_id", MySqlDbType.Int64).Value = grurrah_id;
                cmd.Parameters.Add("user_id", MySqlDbType.VarChar, 36).Value = user_id;
                cmd.Parameters.Add("text", MySqlDbType.Text).Value = text;

                cmd.ExecuteNonQuery();
            }
        }
    }

}

